package net.doglog;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class PostServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		long postId = BlogUtil.parseId(req.getPathInfo());
		Post post = PostUtil.getPost(postId);
		req.setAttribute("post", post);
		req.getRequestDispatcher("/post.jsp").forward(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		long postId = BlogUtil.parseId(req.getPathInfo());
		Post post = PostUtil.getPost(postId);
		String parent = req.getParameter("parent");
		String speaker = req.getParameter("speaker");
		String content = req.getParameter("content");
		try {
			Comment comment = PostUtil.newComment(post, parent, speaker, content);
			resp.sendRedirect("/post/" + postId + "#" + comment.getId());
		} catch (Throwable th) {
			req.setAttribute("parent", parent);
			req.setAttribute("speaker", speaker);
			req.setAttribute("content", content);
			req.setAttribute("error", th.getMessage());
			req.setAttribute("post", post);
			req.getRequestDispatcher("/post.jsp").forward(req, resp);
		}
	}
}
